import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024 —04 —04
 * Time: 11:11
 */


public class Solution {

    /*题1：括号匹配
    * */
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for( int i = 0 ; i < s.length() ; i ++){
            char ch1 = s.charAt(i);
            if(ch1 == '(' || ch1 == '[' || ch1 == '{'){
                stack.push(ch1);
            }else{
                //遇到右括号，ch1为右括号
                if(stack.empty()){
                    return false;
                }
                char ch2 =stack.peek() ;
                if(ch2 =='(' && ch1 == ')' || ch2 =='[' && ch1 == ']' || ch2 =='{' && ch1 == '}'){
                    stack.pop();
                }else{
                    return false;
                }
            }

        }
        //Z字符串遍历完不等于空，说明栈里还有没有匹配上的字符
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}

